# Contributor (MSYS2): David Macek <david.macek.0@gmail.com>
# Maintainer (Arch Linux): Alexander Rødseth <rodseth@gmail.com>
# Contributor (Arch Linux): Angel Velasquez <angvp@archlinux.org>
# Contributor (Arch Linux): Ionut Biru  <ibiru@archlinux.ro>
# Contributor (Arch Linux): William Rea <sillywilly@gmail.com>
# Contributor (Arch Linux): Allan McRae <mcrae_allan@hotmail.com>

_realname=geany
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_realpkgver=2.0.0
pkgver=${_realpkgver%.*}
pkgrel=3
pkgdesc='Fast and lightweight IDE (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.geany.org/'
msys2_repository_url="https://github.com/geany/geany"
msys2_references=(
  'archlinux: geany'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             'perlxml'
             'intltool')
optdepends=("${MINGW_PACKAGE_PREFIX}-geany-plugins: various extra features")
source=("https://github.com/geany/geany/archive/${_realpkgver}/${_realname}-${_realpkgver}.tar.gz"
        #https://download.geany.org/${_realname}-${pkgver}.tar.bz2{,.sig}
        "0002-Use-FHS.patch"
        "0003-export.patch"
        "0004-meson-Prevent-showing-console-after-running-geany-in.patch"
        "0005-meson-Add-icon-with-geany-program.patch")
validpgpkeys=('ACA0246889FB96B63382111724CCD8550E5D1CAE'
              '457990C86F91F33DE05EBF1BCC03633F700990F2')
sha256sums=('a744ab9ae3e58b371de4b50990c44227c499f82e2a8ee6753307ef107748e4df'
            'fd31ca884c41b9f5793fdf83669f664e05d3a97f156d4e9e5f1ccf8dff1b5195'
            '842d06235cbcdf59eb42c164e37cabd86da2399d15c4eb472d11d46b13d3ba2c'
            '77f818fe0a89dbc15f5d351185a865d971e38e06d11327e5261a217a02029772'
            'a735533a74e91df974372dc597c0d4c809dbfc791a5bb20fdd4d01262eed3c77')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_realpkgver}"

  apply_patch_with_msg \
    0002-Use-FHS.patch

  # https://github.com/geany/geany/pull/3067
  apply_patch_with_msg \
    0003-export.patch

  # https://github.com/geany/geany/pull/3811
  apply_patch_with_msg \
    0004-meson-Prevent-showing-console-after-running-geany-in.patch

  # https://github.com/geany/geany/pull/3812
  apply_patch_with_msg \
    0005-meson-Add-icon-with-geany-program.patch
}

build() {
  mkdir -p "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    --buildtype=plain \
    -Dhtml-docs=disabled \
    -Dpdf-docs=disabled \
    -Dapi-docs=disabled \
    -Dmac-integration=disabled \
    -Dvte=false \
    -Dgtkdoc=false \
    -Dplugins=true \
    ../${_realname}-${_realpkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  rm -r "${pkgdir}${MINGW_PREFIX}/share/applications"
  rm -r "${pkgdir}${MINGW_PREFIX}/share/man"

  install -Dm644 "${srcdir}"/${_realname}-${_realpkgver}/src/geany-run-helper.bat \
    "${pkgdir}"${MINGW_PREFIX}/libexec/${_realname}/geany-run-helper.bat
  install -Dm644 "${srcdir}"/${_realname}-${_realpkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
